<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Broadcasts extends Admin_Controller{
	
	public function __construct(){
		parent::__construct();
		$this->loadData('controllerName',$this->router->fetch_class());
	}
	
	public function index(){
		redirect('administrator/broadcasts/manager');
	}
	
	//Quan ly chuong trinh
	public function manager(){
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->loadScript(base_url('js/admin/jquery.modify.js'));
		$this->loadScript(base_url('js/admin/jquery.update.status.js'));
		$this->headTitle('Chương trình phát sóng - Admin Panel');
		
		$this->load->model('Channel_Model');
		$this->loadData('listchannel', $this->Channel_Model->getAllChannel());
		$this->load->model('Programs_Model');
		$channelid = $this->input->get('channelid', true);
		$date = $this->input->get('date', true);
		if(empty($channelid)){
			$firstchannel = $this->Channel_Model->getFirstChannel();
			if($firstchannel){
				$channelid = $firstchannel->ChannelId;
			}else{
				redirect('administrator/channel/add');
			}
		}
		if(empty($date)){
			$date = date('d-m-Y');
		}

		$this->loadData('channelid', $channelid);
		$this->loadData('date', $date);
		$this->loadData('listbroadcasts', $this->Programs_Model->getProgramsByDateAndChannelId($channelid, $date));
		$date_start = $this->Programs_Model->getMinDateOfPrograms();
		if(!$date_start){
			$date_start = date('d-m-Y');
		}
		$this->loadData('date_start', $date_start);
		if($this->input->post()){
			if($this->form_validation->run('formsearch') == true){
				$keysearch = trim($this->input->post('keysearch', true));
				$this->loadData('textkeysearch', $keysearch);
				$this->loadData('listbroadcasts', $this->Programs_Model->searchPrograms($keysearch));
			}

		}
		$this->loadView('administrator/broadcasts/manager');
	
	}
	//Them chuong trinh
	public function add(){
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->load->model('Channel_Model');
		$this->loadData('listchannels', $this->Channel_Model->getAllChannel());
		if($this->input->post()){
			if($this->form_validation->run('broadcasts') == true){							
				$this->load->model('Programs_Model');
				$this->Programs_Model->insertProgram();
				redirect('administrator/broadcasts/manager');	
			
			}		
		}
		$this->loadStylesheet(base_url('plugins/chosen/chosen.css'));
		$this->loadScript(base_url('plugins/chosen/chosen.jquery.min.js'));
		$this->loadScript(base_url('plugins/ckeditor/ckeditor.js'));
		$this->loadScript(base_url('js/jquery.maskMoney.js'));
		$this->loadScript(base_url('js/admin/jquery.fixedListcontrol.js'));
		$this->headTitle('Thêm chương trình - Admin Panel');
		$this->loadView('administrator/broadcasts/add');

	}
	
	//Sua chuong trinh
	public function modify(){
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->load->model('Channel_Model');	
		$this->load->model('Programs_Model');
		$programid = $this->input->get('programid', true);
		$broadcasts = $this->Programs_Model->getProgramByProgramId($programid);
		if($broadcasts){
			if($this->input->post()){
				if($this->form_validation->run('broadcasts') == true){	
					$this->Programs_Model->updateProgram($programid);
					redirect('administrator/broadcasts/manager');
				}		
			}
			$listchannels = $this->Channel_Model->getAllChannel();
			$this->loadData('listchannels',$listchannels);
			$this->loadData('broadcasts', $broadcasts);
			$this->loadStylesheet(base_url('plugins/chosen/chosen.css'));
			$this->loadScript(base_url('plugins/chosen/chosen.jquery.min.js'));
			$this->loadScript(base_url('plugins/ckeditor/ckeditor.js'));
			$this->loadScript(base_url('js/admin/jquery.fixedListcontrol.js'));
			$this->headTitle('Sửa chương trình - Admin Panel');
			$this->loadView('administrator/broadcasts/modify');
		}else{
			redirect('administrator/broadcasts/manager');
		}
	}
	//Doi trang thai chuong trinh
	public function change_status(){
		if($this->input->post()){
			$key = $this->input->post('key', true);
			$status = $this->input->post('status', true);
			$this->load->model('Programs_Model');
			$changeStatus = $this->Programs_Model->changeStatusProgram($key, $status);
			if($changeStatus){
				$json['status'] = $changeStatus;
				$json['success'] = true;
			}else{
				$json['success'] = false;
			}
			echo json_encode($json);
		}
	}
	
	//Xoa chuong trinh
	public function delete(){
		if($this->input->post()){
			$key = $this->input->post('key', true);
			$this->load->model('Programs_Model');		
			if($this->Programs_Model->deleteProgram($key)){
				$json["success"] = true;
			}else{
				$json["success"] = false;
			}
			echo json_encode($json);
		}
	}
	
}